package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    ShoppingCartMapper shoppingCartMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void addCart(ShoppingCartDTO shoppingCartDTO) {
//        1. 判断当前加入到购物车中的商品师傅已经存在
        ShoppingCart shoppingCart = new ShoppingCart();
        Long userId = BaseContext.getCurrentId();//当前用户id
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(userId);

        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);

//        2. 如果存在了,秩序数量加1
        if (shoppingCartList != null && shoppingCartList.size() > 0) {
            ShoppingCart cart = shoppingCartList.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.updateUnmberById(cart);

        } else {
//        3. 如果不存在, 需要插入一条数据
//        4. 判断添加到购物车的是套餐还是菜品
            Long dishId = shoppingCart.getDishId();
            Long setmealId = shoppingCart.getSetmealId();

            if (dishId != null) {
                //菜品....
                Dish dish = dishMapper.getByid(dishId);

                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setName(dish.getName());

            } else {
                // 套餐.....
                Setmeal setmeal = setmealMapper.getByid(setmealId);

                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setName(setmeal.getName());
            }


            //  插入数据  设置默认数量为1 添加当前时间
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.inset(shoppingCart);
        }

    }

    /**
     * 获取购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        //根据userid获取,先拿到userid
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(userId).build();

        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        return list;
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        //根据ID清空购物车先获取id
        Long userId = BaseContext.getCurrentId();

        shoppingCartMapper.delete(userId);

    }

    /**
     * 删除购物车中的一件商品
     * @param shoppingCartDTO
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        Long userId = BaseContext.getCurrentId();//当前用户id
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(userId);
        //设置查询条件，查询当前登录用户的购物车数据
        List<ShoppingCart> List = shoppingCartMapper.list(shoppingCart);

        //注意改查询出来的就是一个套餐的信息所有get(0) 就是这条套餐信息也就是点‘-’的
        if (List != null && List.size() > 0 ){
            shoppingCart  = List.get(0);
        }
        Integer number = shoppingCart.getNumber();//数量
        if (number == 1){
            //当前商品在购物车中的份数为1，直接删除当前记录
            shoppingCartMapper.deleteById(shoppingCart.getId());
        }else {
            //当前商品在购物车中的份数不为1，修改份数即可
             shoppingCart.setNumber(number - 1);
            shoppingCartMapper.updateUnmberById(shoppingCart);
        }
    }
}
